<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

    private $layout = '';
    private $title = '';
    private $meta_http_equiv = array();
    private $meta_name = array();
    private $javascript_variables = array();
    private $javascripts = array();
    private $stylesheets = array();
    private $data = array();

    public function __construct() {
	parent::__construct();
	$this->load->library('session');
    }

    protected function set_layout($layout) {
	if (!empty($layout)) {
	    $this->layout = $layout;
	}
    }

    protected function set_title($title) {
	if (!empty($title)) {
	    $this->title = $title;
	}
    }

    protected function set_meta($type, $key, $value) {
	if ($type == 'name') {
	    $this->meta_name[$key] = $value;
	} else if ($type == 'http-equiv') {
	    $this->meta_http_equiv[$key] = $value;
	}
    }

    protected function set_javascript_variable($key, $value) {
	$this->javascript_variables[$key] = $value;
    }

    protected function load_javascripts($javascripts) {
	if (!empty($javascripts)) {
	    if (is_array($javascripts)) {
		$this->javascripts = array_merge($this->javascripts, $javascripts);
	    } else {
		array_push($this->javascripts, $javascripts);
	    }
	}
    }

    protected function load_stylesheets($stylesheets) {
	if (!empty($stylesheets)) {
	    if (is_array($stylesheets)) {
		$this->stylesheets = array_merge($this->stylesheets, $stylesheets);
	    } else {
		array_push($this->stylesheets, $stylesheets);
	    }
	}
    }

    protected function set_data($data = array()) {
	$this->data = array_merge($this->data, $data);
    }

    protected function rend($template, $data = array()) {
	$data['l_javascripts'] = $this->javascripts;
	$data['l_stylesheets'] = $this->stylesheets;
	$data['l_title'] = $this->title;
	$data['l_meta_name'] = $this->meta_name;
	$data['l_meta_http_equiv'] = $this->meta_http_equiv;
	$data['l_javascript_variables'] = $this->javascript_variables;

	if (empty($this->layout)) {
	    $this->load->view($template, array_merge($data, $this->data));
	} else {
	    $data['l_template'] = $template;
	    $this->load->view($this->layout, array_merge($data, $this->data));
	}
    }

    protected function json_back($status, $msg = '', $data = array()) {
	header('Content-type: application/json; charset=utf-8');
	echo json_encode(array(
	    'status' => $status,
	    'msg' => $msg,
	    'data' => $data
	));
	exit(0);
    }

    protected function upload_back($status, $msg = '', $data = array()) {
	$json_data = json_encode($data);
	echo "<script type=\"text/javascript\">\n
                    this.top.window.uploadCallback('{$status}','{$msg}','{$json_data}');\n
              </script>\n";
	exit(0);
    }

    public function _output($output) {
	//替换模版中的静态文件路径
	$output = preg_replace("/((:?src|href)=\")\/assets/", '$1' . SITE_URL . '/assets', $output);
	$output = preg_replace("/((:?src|href|action)=\")\/(?!(:?assets|http))/", '$1' . SITE_URL . '/index.php/', $output);

	echo $output;
    }

}